<?php

namespace App\Http\Controllers\Admin;

use App\CodeResponse;
use App\Http\Controllers\Controller;

use App\Models\CourseSectionWork;

use App\Models\StudentWork;
use App\Services\ClassGradeService;
use App\Services\CourseSectionWorkService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class CourseSectionWorkController extends AdminController
{
    //添加
    public function  add(Request $request){
        $validator = Validator::make($request->all(), [
            'course_id' => 'required|integer|digits_between:1,20|min:1',
            'users_id' => 'required|integer|digits_between:1,20|min:1',
            'course_section_id' => 'required|integer|digits_between:1,20|min:1',
            'name' => 'required|string',
            'work_file' => 'required|string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        CourseSectionWork::create($request->all());
        return $this->success();
    }

    //修改
    public function  update(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
            'users_id' => 'required|integer|digits_between:1,20|min:1',
            'course_id' => 'required|integer|digits_between:1,20|min:1',
            'course_section_id' => 'required|integer|digits_between:1,20|min:1',
            'name' => 'required|string',
            'work_file' => 'required|string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $putData = $request->except(['_method','_token']);
        $courseSectionWork = new CourseSectionWork();
        $courseSectionWork->find($request->id)->update($putData);
        return $this->success();
    }
    //列表
    public function  list(Request $request){
        $validator = Validator::make($request->all(), [

            'sort' => 'string',
            'order' => 'string',
            'course_id' => 'integer|digits_between:1,20|min:1',
            'course_section_id' => 'integer|digits_between:1,20|min:1',
            'school_id' => 'integer|digits_between:1,20|min:1',
            'class_grades_id' => 'integer|digits_between:1,20|min:1',
            'name' => 'string',

        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $sort = $request->input('sort','created_at');
        $order = $request->input('order','asc');
        $name = $request->input('name');
        $school_id = $request->input('school_id');
        $class_grades_id = $request->input('class_grades_id');
        $course_id = $request->input('course_id');
        $course_section_id = $request->input('course_section_id');


        $list = CourseSectionWorkService::getInstance()->list($sort,$order,$school_id,$class_grades_id,$course_id, $course_section_id, $name);
        return $this->success($list);
    }

    //删除
    public function  delete(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $courseSectionWork = CourseSectionWork::find($request->id);
        $courseSectionWork->delete();
        return $this->success();
    }

    //学生添加作业
    public function addStudentWork(Request $request){
        $validator = Validator::make($request->all(), [
            'users_id' => 'required|integer|digits_between:1,20|min:1',
            'course_id' => 'required|integer|digits_between:1,20|min:1',
            'course_section_id' => 'required|integer|digits_between:1,20|min:1',
            'work_name' => 'required|string',
            'work_file' => 'required|string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        StudentWork::create($request->all());
        return $this->success();
    }
    //学生修改作业
    public function updateStudentWork(Request $request){
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer|digits_between:1,20|min:1',
            'users_id' => 'required|integer|digits_between:1,20|min:1',
            'course_id' => 'required|integer|digits_between:1,20|min:1',
            'course_section_id' => 'required|integer|digits_between:1,20|min:1',
            'work_name' => 'required|string',
            'work_file' => 'required|string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }

        $studentWork = new StudentWork();
        $putData = $request->except(['_method','_token']);
        $studentWork->find($request->id)->update($putData);
        return $this->success();
    }
    //学生作业
    public function listStudentWork(Request $request){
        $validator = Validator::make($request->all(), [
            'sort' => 'string',
            'order' => 'string',
            'users_id' => 'integer',
            'course_section_id' => 'integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $sort = $request->input('sort','created_at');
        $order = $request->input('order','asc');
        $users_id = $request->input('users_id');
        $course_section_id = $request->input('course_section_id');


        $list = CourseSectionWorkService::getInstance()->listStudentWork($sort,$order,$users_id, $course_section_id);
        return $this->success($list);
    }

    // 上传作业
    public function upfile(Request $request) {
        $url = config('filesystems.disks.course.url');
        $default = config('filesystems.disks.course.default');
        $pic =  CourseService::getInstance()->upfile($request,$url,$default,'course');
        return $this->success([
                'pic'=>$pic,
            ]
        );
    }
    //获取作业数量
    public function getMost(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'school_id' => 'required|integer|digits_between:1,20|min:1',
            'class_grades_id' => 'required|integer|digits_between:1,20|min:1',
            'course_id' => 'required|integer|digits_between:1,20|min:1',
            'course_section_id' => 'required|integer|digits_between:1,20|min:1',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $school_id = $request->input('school_id');
        $class_grades_id = $request->input('class_grades_id');
        $course_id = $request->input('course_id');
        $course_section_id = $request->input('course_section_id');

        $classGradeStudentNum =  ClassGradeService::getInstance()->getClassGradeStudentNum($school_id,$class_grades_id);
        $sectionWorkNum = CourseSectionWorkService::getInstance()->getSectionWorkNum($course_id,$course_section_id);

        $arr=[
            'sectionWorkNum' =>$sectionWorkNum,//已经添加作业人数
            'notSectionWorkNum' => ($classGradeStudentNum-$sectionWorkNum),//未添加作业人数
            'classStudentNum' => $classGradeStudentNum //班级人数
        ];
        return $this->success($arr);
    }
}
